package cn.javabb.admin.controller.pay;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.pay.GoodsDO;
import cn.javabb.core.entity.pay.OrderDO;
import cn.javabb.core.query.OrderQuery;
import cn.javabb.core.service.pay.GoodsService;
import cn.javabb.core.service.pay.OrderService;
import cn.javabb.log.action.DeleteAction;
import cn.javabb.log.action.StateAction;
import cn.javabb.log.annotation.Log;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/09/14 14:46
 */
@Controller
@RequestMapping("/admin/order")
public class OrderAdminController extends BaseController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    public String listPage(Model model){
        List<GoodsDO> goodsList = goodsService.queryAll();
        model.addAttribute("goodsList", goodsList);
        return "admin/pay/orderList";
    }
    @GetMapping("/save")
    public String savePage(){
        return "admin/pay/orderSave";
    }

    @ResponseBody
    @GetMapping("/")
    public Object list(OrderQuery orderQuery) {
        Page p =orderService.queryPageBy(orderQuery);
        return ResponseModel.table(p);
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @ResponseBody
    @DeleteMapping("/{ids}")
    @Log(name = "订单删除",action = DeleteAction.class)
    public Object delete(@PathVariable String ids){
        if(StringUtils.isBlank(ids)){
            return ResponseModel.Failure("参数错误");
        }
        List<String> idList = ListUtils.stringToListString(ids);
        int num = orderService.deleteByIds(OrderDO.class, "orderId", idList);
        if(num>0){
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }
    @ResponseBody
    @PostMapping("/updateState")
    @Log(name="订单状态",action = StateAction.class)
    public Object updateState(String ids,Integer state){
        if(orderService.updateState(ids,state)){
            return ResponseModel.Success("状态更新成功");
        }
        return ResponseModel.Failure("状态更新失败");
    }
}
